<template>
  <div
    ref="dragDiv"
    class="drag"
  >
    <div class="drag_bg"></div>
    <div class="drag_text">{{ confirmWords }}</div>
    <div
      ref="moveDiv"
      :class="{'handler_ok_bg':confirmSuccess}"
      class="handler handler_bg"
      style="position: absolute;top: 0px;"
      @mousedown="mouseDownFn($event)"
    ></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      beginClientX: 0 /* 距离屏幕左端距离*/,
      mouseMoveState: false /* 触发拖动状态  判断*/,
      maxWidth: "" /* 拖动最大宽度，依据滑块宽度算出来的*/,
      confirmWords: "向右拖动滑块验证" /* 滑块文字*/,
      confirmSuccess: false /* 验证成功判断*/
    };
  },
  mounted() {
    this.maxWidth =
      this.$refs.dragDiv.clientWidth - this.$refs.moveDiv.clientWidth;
    if (this.maxWidth) {
      document
        .getElementsByTagName("html")[0]
        .addEventListener("mousemove", this.mouseMoveFn);
      document
        .getElementsByTagName("html")[0]
        .addEventListener("mouseup", this.moseUpFn);
    }
  },
  methods: {
    init() {
      this.mouseMoveState = false /* 触发拖动状态  判断*/
      // this.maxWidth = "" /* 拖动最大宽度，依据滑块宽度算出来的*/
      this.confirmWords = "向右拖动滑块验证" /* 滑块文字*/
      this.confirmSuccess = false /* 验证成功判断*/
      if (document.getElementsByClassName("handler")[0]) {
        document.getElementsByClassName("handler")[0].style.left = 0 + "px";
        document.getElementsByClassName("drag_bg")[0].style.width = 0 + "px";
      }
      this.beginClientX = 0 /* 距离屏幕左端距离*/
      this.maxWidth =
      this.$refs.dragDiv.clientWidth - this.$refs.moveDiv.clientWidth;
      if (this.maxWidth) {
        document
          .getElementsByTagName("html")[0]
          .addEventListener("mousemove", this.mouseMoveFn);
        document
          .getElementsByTagName("html")[0]
          .addEventListener("mouseup", this.moseUpFn);
      }
    },
    // mousedown 事件
    mouseDownFn: function(e) {
      this.mouseMoveState = true;
      if (!this.confirmSuccess) {
        this.beginClientX = e.clientX;
        e.preventDefault && e.preventDefault(); // 阻止文字选中等 浏览器默认事件
      }
    },
    // 验证成功函数
    successFunction() {
      this.confirmSuccess = true;
      this.confirmWords = "验证通过";
      if (window.addEventListener) {
        document
          .getElementsByTagName("html")[0]
          .removeEventListener("mousemove", this.mouseMoveFn);
        document
          .getElementsByTagName("html")[0]
          .removeEventListener("mouseup", this.moseUpFn);
      } else {
        document
          .getElementsByTagName("html")[0]
          .removeEventListener("mouseup", () => {});
      }
      if (document.getElementsByClassName("handler")[0]) {
        document.getElementsByClassName("drag_text")[0].style.color = "#fff";
        document.getElementsByClassName("handler")[0].style.left =
          this.maxWidth + "px";
        document.getElementsByClassName("drag_bg")[0].style.width =
          this.maxWidth + "px";
      }
    },
    // mousemove事件
    mouseMoveFn(e) {
      // console.log(e, this.maxWidth, this.mouseMoveState);
      if (this.mouseMoveState) {
        let width = e.clientX - this.beginClientX;
        if (width > 0 && width < this.maxWidth) {
          if (document.getElementsByClassName("handler")[0]) {
            document.getElementsByClassName("handler")[0].style.left =
              width + "px";
            document.getElementsByClassName("drag_bg")[0].style.width =
              width + "px";
          }
        } else if (width > this.maxWidth - 30) {
          this.successFunction();
        }
      }
    },
    // mouseup事件
    moseUpFn(e) {
      this.mouseMoveState = false;
      var width = e.clientX - this.beginClientX;
      if (width < this.maxWidth) {
        if (document.getElementsByClassName("handler")[0]) {
          document.getElementsByClassName("handler")[0].style.left = 0 + "px";
          document.getElementsByClassName("drag_bg")[0].style.width = 0 + "px";
        }
      }
    }
  }
};
</script>
<style lang="scss" scoped>
@import "~@/styles/variables";
.drag {
  position: relative;
  background-color: #e8e8e8;
  width: 100%;
  height: 40px;
  line-height: 40px;
  text-align: center;
  border: 1px solid #ccc;
  border-radius: 4px;
}
.handler {
  width: 40px;
  height: 38px;
  border: 1px solid #ccc;
  border-radius: 4px;
  cursor: move;
}
.handler_bg {
  background: $appMainTextColor;

  background: url("")
    no-repeat center;
}
.handler_ok_bg {
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
  background: $appMainTextColor;
  background: url("")
    no-repeat center;
}
.drag_bg {
  background-color: #7ac23c;
  height: 38px;
  width: 0px;
  border-radius: 4px 0 0 4px;
}
.drag_text {
  position: absolute;
  top: 0px;
  width: 100%;
  text-align: center;
  -moz-user-select: none;
  -webkit-user-select: none;
  user-select: none;
  -o-user-select: none;
  -ms-user-select: none;
}
</style>
